//
//  MoboPlayerDecoder.h
//  MoboPlayer
//
//  Created by Kolyvan on 15.10.12.
//  Copyright (c) 2012 Konstantin Boukreev . All rights reserved.
//
//  https://github.com/kolyvan/MoboPlayer
//  this file is part of MoboPlayer
//  MoboPlayer is licenced under the LGPL v3, see lgpl-3.0.txt

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <Accelerate/Accelerate.h>
#import <libavformat/avformat.h>
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/pixdesc.h"
#import <UIKit/UIKit.h>
#import <libavfilter/avfilter.h>
#include <libavutil/opt.h>
extern NSString * MoboPlayerErrorDomain;

typedef enum {
    
    MoboPlayerErrorNone,
    MoboPlayerErrorOpenFile,
    MoboPlayerErrorStreamInfoNotFound,
    MoboPlayerErrorStreamNotFound,
    MoboPlayerErrorCodecNotFound,
    MoboPlayerErrorOpenCodec,
    MoboPlayerErrorAllocateFrame,
    MoboPlayerErroSetupScaler,
    MoboPlayerErroReSampler,
    MoboPlayerErroUnsupported,
    
} MoboPlayerError;

typedef enum {
    
    MoboPlayerFrameTypeAudio,
    MoboPlayerFrameTypeVideo,
    MoboPlayerFrameTypeArtwork,
    MoboPlayerFrameTypeSubtitle,
    
} MoboPlayerFrameType;

typedef enum {
        
    MoboVideoFrameFormatRGB,
    MoboVideoFrameFormatYUV,
    
} MoboVideoFrameFormat;

@interface MoboPlayerFrame : NSObject
@property (readonly, nonatomic) MoboPlayerFrameType type;
@property (readonly, nonatomic) CGFloat position;
@property (readonly, nonatomic) CGFloat duration;
@end

@interface MoboAudioFrame : MoboPlayerFrame
@property (readonly, nonatomic, strong) NSData *samples;
@end

@interface MoboVideoFrame : MoboPlayerFrame
@property (readonly, nonatomic) MoboVideoFrameFormat format;
@property (readonly, nonatomic) NSUInteger width;
@property (readonly, nonatomic) NSUInteger height;
@end

@interface MoboVideoFrameRGB : MoboVideoFrame
@property (readonly, nonatomic) NSUInteger linesize;
@property (readonly, nonatomic, strong) NSData *rgb;
- (UIImage *) asImage;
@end

@interface MoboVideoFrameYUV : MoboVideoFrame
@property (readonly, nonatomic, strong) NSData *luma;
@property (readonly, nonatomic, strong) NSData *chromaB;
@property (readonly, nonatomic, strong) NSData *chromaR;
@end

@interface MoboArtworkFrame : MoboPlayerFrame
@property (readonly, nonatomic, strong) NSData *picture;
- (UIImage *) asImage;
@end

@interface MoboSubtitleFrame : MoboPlayerFrame
@property (readonly, nonatomic, strong) NSString *text;
@end

//字幕信息管理类
@interface SubtitleDataClass : NSObject
@property (readwrite,nonatomic) AVFormatContext     *fmt_ctx;
@property (readonly,nonatomic) AVCodecContext      *CodecCtx;
@property (readwrite,nonatomic) AVStream            *subtitlestream;
@property (readonly,nonatomic) NSUInteger           insubtitle_index;
@property (readonly,nonatomic) NSUInteger           outsubtitle_index;
@property (readwrite,nonatomic) BOOL                 is_decoding;       //是否被选择解码
@property (readwrite,nonatomic) BOOL                 is_seaching;       //是否是外置字幕
@property (readwrite,nonatomic) NSString*            filepath;
@property (readwrite,nonatomic) NSMutableArray*      avsubtitlearry;
@end

typedef BOOL(^MoboPlayerDecoderInterruptCallback)();

@interface MoboPlayerDecoder : NSObject

@property (readonly, nonatomic, strong) NSString *path;
@property (readonly, nonatomic) BOOL isEOF;
@property (readwrite,nonatomic) CGFloat position;
@property (readonly, nonatomic) CGFloat duration;
@property (readonly, nonatomic) CGFloat fps;
@property (readonly, nonatomic) CGFloat sampleRate;
@property (readwrite, nonatomic) CGFloat playerspeed;
@property (readonly, nonatomic) NSUInteger frameWidth;
@property (readonly, nonatomic) NSUInteger frameHeight;
@property (readonly, nonatomic) NSUInteger audioStreamsCount;
@property (readwrite,nonatomic) NSInteger selectedAudioStream;
@property (readwrite,nonatomic) NSArray* subtitleStreamsNsArray;
@property (readwrite,nonatomic) NSArray*  audioStreamsNSArray;
@property (readonly, nonatomic) NSUInteger subtitleStreamsCount;
@property (readwrite,nonatomic) NSArray *selectedSubtitleStreams;
@property (readwrite,nonatomic) NSMutableArray * SubtitleStreamCodecMangeArray;
@property (readonly, nonatomic) BOOL validVideo;
@property (readonly, nonatomic) BOOL validAudio;
@property (readonly, nonatomic) BOOL validSubtitles;
@property (readonly, nonatomic, strong) NSDictionary *streaminfo;
@property (readonly, nonatomic, strong) NSString *videoStreamFormatName;
@property (readonly, nonatomic) BOOL isNetwork;
@property (readonly, nonatomic) CGFloat startTime;
@property (readwrite, nonatomic) BOOL disableDeinterlacing;
@property (readwrite, nonatomic, strong) MoboPlayerDecoderInterruptCallback interruptCallback;

+ (id) movieDecoderWithContentPath: (NSString *) path
                             error: (NSError **) perror;
//保存截图
-(UIImage *)savepicture:(NSInteger) pwidth
                 height:(NSInteger) pheight;

- (BOOL) openFile: (NSString *) path
            error: (NSError **) perror;

-(void) closeFile;

- (BOOL) setSelectedSubtitleStream:(NSArray *)selected;

- (BOOL) setupVideoFrameFormat: (MoboVideoFrameFormat) format;

- (NSArray *) decodeSubtitleFrames: (CGFloat) minDuration;
- (NSArray *) decodeFrames: (CGFloat) minDuration;
-(void) setspeedfilter:(Float64) speed;
-(BOOL ) AddSubtitleFile: (NSString *) path
                Filename:(NSString *) name;

@end

@interface MoboPlayerSubtitleASSParser : NSObject

+ (NSArray *) parseEvents: (NSString *) events;
+ (NSArray *) parseDialogue: (NSString *) dialogue
                  numFields: (NSUInteger) numFields;
+ (NSString *) removeCommandsFromEventText: (NSString *) text;

@end
